package com.czz.manage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czz.manage.client.OssClient;
import com.czz.manage.client.VodClient;
import com.czz.manage.entity.Period;
import com.czz.manage.mapper.PeriodMapper;
import com.czz.manage.service.PeriodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: PeriodServiceImpl
 * @Author Czz
 * @Date 2020/1/22
 * @Time 17:22
 **/
@Service
public class PeriodServiceImpl extends ServiceImpl<PeriodMapper, Period> implements PeriodService {

    @Autowired
    PeriodMapper periodMapper;

    @Autowired
    OssClient ossClient;

    @Autowired
    VodClient vodClient;

    @Override
    @Transactional
    public void deletePeriodById(String id) {

        String OSS_URL = "http://czz-education.oss-cn-shenzhen.aliyuncs.com/";

        //判断课时类型
        Period period = new Period();
        period = periodMapper.selectById(id);

        //类型为文档
        if (period.getDoc() == 1){
            String docUrl = period.getDocUrl().replace(OSS_URL,"").replaceAll("/","&&");
            ossClient.deleteOssFile(docUrl).getMessage();
        }

        //类型为视频
        if (period.getVideo() == 1){
            String videoId = period.getVideoId();
            vodClient.deleteVodById(videoId);
        }

        periodMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void updatePeriodById(Period period) {
//        String OSS_URL = "http://czz-education.oss-cn-shenzhen.aliyuncs.com/";
//
//        //判断课时类型是否更改
//        Period temp = periodMapper.selectById(period.getPeriodId());
//
//        //课时类型更改为文档，删除视频点播中视频
//        if ((temp.getDoc() != period.getDoc()) && period.getDoc() == 1){
//            vodClient.deleteVodById(temp.getVideoId());
//        }
//
//        //课时类型更改为视频，删除oss中文档
//        if ((temp.getVideo() != period.getVideo()) && period.getVideo() == 1){
//            String docUrl = period.getDocUrl().replace(OSS_URL,"").replaceAll("/","&&");
//            ossClient.deleteOssFile(docUrl).getMessage();
//        }
        //更改数据库信息
        periodMapper.updateById(period);
    }
}
